package com.bwie.home.backstage.service.impl;

import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.home.common.utils.PageUtils;
import com.bwie.home.common.utils.Query;

import com.bwie.home.backstage.dao.RegionDao;
import com.bwie.home.backstage.entity.RegionEntity;
import com.bwie.home.backstage.service.RegionService;


@Service("regionService")
public class RegionServiceImpl extends ServiceImpl<RegionDao, RegionEntity> implements RegionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RegionEntity> page = this.page(
                new Query<RegionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getProvinces(Map<String, Object> params) {
        IPage<RegionEntity> page = this.page(
                new Query<RegionEntity>().getPage(params),
                new QueryWrapper<RegionEntity>().eq("parent_id",0)
        );
        List<RegionEntity> list = page.getRecords();
        for (RegionEntity regionEntity : list) {
            //计算启用城市个数
            int start = 0;
            //计算禁用城市个数
            int end = 0;
            List<RegionEntity> selectList = baseMapper.selectList(new QueryWrapper<RegionEntity>().eq("parent_id", regionEntity.getRegionId()));
            for (RegionEntity entity : selectList) {
                if (entity.getRegionStatus() == 1){
                    start+=1;
                }else {
                    end+=1;
                }
            }
            regionEntity.setStartCount(start);
            regionEntity.setEndCount(end);
        }
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public PageUtils getCities(Integer regionId, Map<String, Object> params) {
        IPage<RegionEntity> page = this.page(
                new Query<RegionEntity>().getPage(params),
                new QueryWrapper<RegionEntity>().eq("parent_id",regionId)
        );
        List<RegionEntity> list = page.getRecords();
        for (RegionEntity entity : list) {
            String district = "";
            List<RegionEntity> regionEntities = baseMapper.selectList(new QueryWrapper<RegionEntity>().eq("parent_id",entity.getRegionId()));
            for (RegionEntity e : regionEntities) {
                district += "," + e.getRegionName();
            }
            district = district.substring(1);
            entity.setDistricts(district);
        }
        return new PageUtils(page);
    }

    @Override
    public List<RegionEntity> getRegionAllCities(Integer regionId) {
        List<RegionEntity> list = baseMapper.selectList(new QueryWrapper<RegionEntity>().eq("parent_id", regionId));
        for (RegionEntity entity : list) {
            List<RegionEntity> regionEntities = baseMapper.selectList(new QueryWrapper<RegionEntity>().eq("parent_id",entity.getRegionId()));
            entity.setChildren(regionEntities);
        }
        return list;
    }

}